﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class CT_DangNhapBLL
    {
        QuanLyChamCongDataContext DB = new QuanLyChamCongDataContext();
        public void ThemTaiKhoan(CT_DANGNHAP tk)
        {
            DB.CT_DANGNHAPs.InsertOnSubmit(tk);
            DB.SubmitChanges();
        }

        public void SuaTaiKhoan(CT_DANGNHAP tkm)
        {
            CT_DANGNHAP tkc = DB.CT_DANGNHAPs.SingleOrDefault(a => a.MaNV == tkm.MaNV);
            if (tkc != null)
            {
                if (tkm.MatKhau != null)
                {
                    tkc.MatKhau = tkm.MatKhau;
                }

                if (tkm.Active != null)
                {
                    tkc.Active = tkm.Active;
                }
                
                DB.SubmitChanges();
            }
        }

        public CT_DANGNHAP LayTKTuNV(int _maNV)
        {
            CT_DANGNHAP tk = DB.CT_DANGNHAPs.SingleOrDefault(a => a.MaNV == _maNV);
            return tk;
        }
        public CT_DANGNHAP LayMaNVTuTK(string _TaiKhoan)
        {
            CT_DANGNHAP tk = DB.CT_DANGNHAPs.SingleOrDefault(a => a.TenDangNhap == _TaiKhoan);
            return tk;
        }
        public string ktTenDangNhapTonTai(string _TenDangNhap, int _maNV)
        {
            if (DB.CT_DANGNHAPs.Any(m => m.TenDangNhap == _TenDangNhap))
                return "Tên Đăng Nhập";
            if(DB.CT_DANGNHAPs.Any(m => m.MaNV == _maNV))
                return "Mã Nhân Viên";
            else
                return null;
        }
        public string ktTenDangNhap(string _TenDangNhap)
        {
            if (DB.CT_DANGNHAPs.Any(a => a.TenDangNhap == _TenDangNhap))
            {
                return "Tên đăng nhập";
            }
            else
            {
                return null;
            }
        }
        public List<NHANVIEN> layDSNhanVienTheoPB(int _MaPB)
        {
            return DB.NHANVIENs.Where(nv => nv.MaPB == _MaPB).ToList();
        }

        public bool KiemTraTrangThai(int manv)
        {
            if (DB.CT_DANGNHAPs.Any(a => a.MaNV == manv && a.Active == 1))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
